package com.xhtt.modules.app.controller.inspection;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.DownloadFile;
import com.xhtt.common.utils.R;
import com.xhtt.common.utils.ReadExcel;
import com.xhtt.modules.inspection.entity.InsLabel;
import com.xhtt.modules.inspection.entity.InsPointLabel;
import com.xhtt.modules.inspection.service.IInsLabelService;
import com.xhtt.modules.inspection.service.IInsPointLabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.Console;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 巡检标签 前端控制器
 * </p>
 *
 * @author xj
 * @since 2019-01-18
 */
@RestController
@RequestMapping("app/ins-label")
@Api(value = "巡检-标签", tags = "巡检-标签")
public class InsLabelController {

    @Autowired
    private IInsLabelService insLabelService;

    @Autowired
    private IInsPointLabelService insPointLabelService;

    @GetMapping(value = "/list")
    @ApiOperation("获取巡检标签列表(web/app)")
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insLabelService.queryPage(params));
    }

    @GetMapping(value = "/labelNames")
    @ApiOperation("获取巡检标签名称集合(web)")
    public R labelNames() {
        List<InsLabel> insLabelList = insLabelService.list(new LambdaQueryWrapper<InsLabel>().select(InsLabel::getName));
        return R.ok().put(Constant.DATA, insLabelList);
    }

    @PostMapping(value = "/add")
    @ApiOperation("新增巡检标签(web/app)")
    public R add(@RequestBody InsLabel insLabel) {
        insLabelService.add(insLabel);
        return R.ok();
    }

    @PostMapping(value = "/delete")
    @ApiOperation("删除巡检标签(web)")
    public R delete(@RequestBody Long[] ids) {
        int count = insPointLabelService
                .count(new LambdaQueryWrapper<InsPointLabel>()
                        .in(InsPointLabel::getLabelId, Arrays.asList(ids)));
        if (count > 0) {
            throw new RRException("请先将标签与检测点解绑后再进行删除");
        }
        insLabelService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @PostMapping(value = "/update")
    @ApiOperation("修改巡检标签(web/app)")
    public R update(@RequestBody InsLabel insLabel) {
        int codeExist = insLabelService.count(new LambdaQueryWrapper<InsLabel>().eq(InsLabel::getCode, insLabel.getCode()).ne(InsLabel::getId, insLabel.getId()));
        int nameExist = insLabelService.count(new LambdaQueryWrapper<InsLabel>().eq(InsLabel::getName, insLabel.getName()).ne(InsLabel::getId, insLabel.getId()));
        if (codeExist > 0) {
            throw new RRException(RRExceptionEnum.LABEL_CODE_EXIST);
        }
        if (nameExist > 0) {
            throw new RRException(RRExceptionEnum.LABEL_NAME_EXIST);
        }
        insLabelService.updateById(insLabel);
        return R.ok();
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation("获取巡检标签(web/app)")
    public R get(@PathVariable("id") Long id) {
        return R.ok().put(Constant.DATA, insLabelService.getById(id));
    }


    @PostMapping("/upload")
    @ApiOperation("巡检管理-标签导入")
    public R upload(MultipartFile file) {

        Map<Long, Map<Integer, String>> map = ReadExcel.readExcel(file, 1);

        if (CollectionUtils.isEmpty(map)) {
            throw new RRException("文件内容不能为空");
        }
        insLabelService.upload(map);
        return R.ok();
    }

    @GetMapping(value = "/mouldExport")
    @ApiOperation("标签模板下载")
    public void mouldExport(HttpServletResponse response) {
        String fileName = "标签信息导入模板.xls";
        String path = "templates/outLabel.xls";
        DownloadFile.exportFile(fileName, path, response);

    }

    @GetMapping(value = "/export")
    @ApiOperation("标签导出")
    public void export(HttpServletResponse response, @RequestParam Map<String, Object> params) {
        insLabelService.exportExcel(response, params);
    }

}
